<template>
  <el-menu
      style="min-height: 100vh;"
      default-active="1-4-1"
      class="el-menu-vertical-demo"
      @open="handleOpen"
      @close="handleClose"
      :collapse="isCollapse"
      background-color="#FCE4EC"
      text-color="#333"
      active-text-color="#333"
  >
    <!-- 要放到导航栏里面 -->
    <h3>{{ isCollapse ? "个人中心" : "个人中心" }}</h3>
    <!-- 观察数据,我们发现name是唯一标识 -->
    <!-- 查看文档,index是唯一标识 -->
    <el-menu-item @click="clickItem(item)" v-for="item in noChildren" :key="item.name" :index="item.name">
      <!-- 这里是字体图标,用模板字符串拼接,注意要动态绑定 -->
      <i :class="`el-icon-${item.icon}`"></i>
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>
    <el-submenu v-for="item in hasChildren" :key="item.label" :index="item.label">
      <template slot="title">
        <i :class="`el-icon-${item.icon}`"></i>
        <span slot="title">{{ item.label }}</span>
      </template>
      <el-menu-item-group v-for="subItem in item.children" :key="subItem.name">
        <el-menu-item @click="clickItem(subItem)" :index="subItem.name">{{ subItem.label }}</el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>

<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 100vh;
  transition: width 0.3s ease, background-color 0.3s ease, transform 0.3s ease, opacity 0.3s ease;
}

.el-menu {
  height: 100vh;
  border-right: none;

  h3 {
    text-align: center;
    line-height: 48px;
    color: #151313;
    font-size: 16px;
    font-weight: 400;
    transition: opacity 0.3s ease;
  }
}

.el-menu-vertical-demo {
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: width 0.3s ease, background-color 0.3s ease, transform 0.3s ease, opacity 0.3s ease;
}

.el-menu-item, .el-submenu__title {
  background-color: transparent;
  transition: background-color 0.5s ease, transform 0.5s ease, opacity 0.5s ease;
}

.el-menu-item:hover, .el-submenu__title:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.el-menu-item.is-active {
  background-color: rgba(255, 255, 255, 0.2);
}

.el-menu-item, .el-submenu .el-menu-item {
  color: #333;
  transition: opacity 0.5s ease;
}

.el-submenu__icon-arrow {
  color: #333;
  transition: transform 0.5s ease;
}

// 添加关键帧动画
@keyframes slideIn {
  0% {
    transform: translateX(-20px);
    opacity: 0;
  }
  25% {
    transform: translateX(-15px);
    opacity: 0.25;
  }
  50% {
    transform: translateX(-10px);
    opacity: 0.5;
  }
  75% {
    transform: translateX(-5px);
    opacity: 0.75;
  }
  100% {
    transform: translateX(0);
    opacity: 1;
  }
}

@keyframes slideOut {
  0% {
    transform: translateX(0);
    opacity: 1;
  }
  25% {
    transform: translateX(-5px);
    opacity: 0.75;
  }
  50% {
    transform: translateX(-10px);
    opacity: 0.5;
  }
  75% {
    transform: translateX(-15px);
    opacity: 0.25;
  }
  100% {
    transform: translateX(-20px);
    opacity: 0;
  }
}


.el-menu-vertical-demo.el-menu--collapse h3 {
  animation: slideOut 0.3s ease forwards;
}

.el-menu-vertical-demo:not(.el-menu--collapse) h3 {
  animation: slideIn 0.3s ease forwards;
}
</style>

<script>
export default {
  data() {
    return {};
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    clickItem(item) {
      if (this.$route.path !== item.path && !(this.$route.path === '/home' && (item.path === '/'))) {
        this.$router.push(item.path)
      }
      this.$store.commit('SelectMenu', item)
    }
  },
  computed: {
    noChildren() {
      return this.MenuData.filter(item => !item.children)
    },
    hasChildren() {
      return this.MenuData.filter(item => item.children)
    },
    isCollapse() {
      return this.$store.state.tab.isCollapse
    },
    MenuData() {
      return this.$store.state.tab.menu
    }
  }
}
</script>
